
// 无法修改 https request header 中的 host ， gitee 又强制使用 https，
// 所以无法直接访问灰度环境了。需要使用者自行修改本地 host

// chrome.webRequest.onBeforeSendHeaders.addListener(
//   (data) => {
//     console.log(data);
//     data.requestHeaders.push({ name: 'HOST', value: 'gray.gitee.com' });
//     data.requestHeaders.push({ name: 'Origin', value: 'gray.gitee.com' });
//     data.requestHeaders.push({ name: 'referer', value: 'gray.gitee.com' });
//     data.requestHeaders.push({ name: 'custom', value: 'gray.gitee.com' });
//     data.requestHeaders.push({ name: 'User-Agent', value: '' });
//     return { requestHeaders: data.requestHeaders };
//   },
//   {
//     urls: ['http://106.13.250.164/*'],
//   },
//   ['extraHeaders', 'requestHeaders']
// );

// https 无效
// chrome.declarativeNetRequest.updateDynamicRules({
//   addRules: [{
//     "id": 883,
//     "priority": 3,
//     "action": {
//       "type": "modifyHeaders",
//       "requestHeaders": [
//         { "header": "Referer", "operation": "set", "value": "https://gitee.com" },
//         { "header": "X-Forwarded-Host", "operation": "set", "value": "gitee.com" },
//         { "header": "HOST", "operation": "set", "value": "gitee.com:443" },
//         { "header": "Origin", "operation": "set", "value": "https://gitee.com" },
//         { "header": "User-Agent", "operation": "set", "value": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.35" },
//       ]
//     },
//     "condition": {
//       "urlFilter": "http://106.13.250.164/*",
//       "resourceTypes": ["xmlhttprequest"]
//     }
//   }],
//   removeRuleIds: [883]
// });

// ; (async () => {

// var myheaders = new Headers(
//   {
//     "Accept": "/",
//     // "Content-Type": "application/json; charset=UTF-8",
//     "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-US;q=0.7,en-GB;q=0.6",
//     "Sec-Fetch-Dest": "document"
//   });
// fetch(
//   'http://106.13.250.164/version.json',
// );
// fetch(
//   'http://106.13.250.164/version.json',
//   {});
// await ((await fetch(
//   'https://106.13.250.164/version.json',
//   {
//     method: 'GET',
//     // credentials: 'include',
//     // headers: myheaders
//     mode: 'cors'
//   }
// ).then((err) => {
//   console.log(err);
// })).text());
// })();



chrome.runtime.onMessage.addListener(async function (request, sender, sendResponse) {
  if (request.type === "page-load") {
    const names = ['https://gitee.com/version.json', 'https://gray.gitee.com/version.json', 'https://e.gitee.com/version.json', 'https://edu.gitee.com/version.json'];
    const data = await Promise.allSettled(
      names.map((item) => fetch(item, {
        mode: 'cors',
        headers: {
          'content-type': 'application/json'
        }
      }).then(async (res) => await res.json()))
    );
    console.log(data);
    chrome.tabs.sendMessage(sender.tab.id, {
      type: "get-version",
      data: {
        gitee: {
          prod: data?.[0]?.value?.version,
          alpha: data?.[1]?.value?.version
        },
        ent: {
          version: data?.[2]?.value?.version,
        },
        edu: {
          version: data?.[3]?.value?.version,
        }
      }
    });
  }
});